﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using PPP.ViewModel.BaseModel;
using System.Windows.Media.Imaging;

namespace PPP.ViewModel.WindowModel {
    /// <summary>
    /// ViewModel for a picture gallery. 
    /// 
    /// <list type="bullet">
    /// <item>
    /// <term>Author</term>
    /// <description>Sebastian Will</description>
    /// </item>
    /// </list>
    /// 
    /// </summary>
    public class GalleryViewModel : PaneViewModel {

        #region Fields

        /// <summary>
        /// The selected picture.
        /// </summary>
        private PicturePropertiesViewModel _selectedPicture = null;

        /// <summary>
        /// The collection of pictures.
        /// </summary>
        private ObservableCollection<PicturePropertiesViewModel> _pictures = null;

        /// <summary>
        /// The count of gallery column count.
        /// </summary>
        private int _galleryColumnCount = DefaultGalleryColumnCount;

        /// <summary>
        /// The gallery thumbnail size.
        /// </summary>
        private int _galleryThumbnailSize = DefaultGalleryThumbnailSize;


        /// <summary>
        /// The gallery size.
        /// </summary>
        private int _gallerySize = DefaultGallerySize;

        /// <summary>
        /// The gallery thumbnail margin size.
        /// </summary>
        private int _galleryThumbnailMarginSize = DefaultGalleryThumbnailMarginSize;
        

        #endregion

        #region Consts
        /// <summary>
        /// Default value of the gallery column count.
        /// </summary>
        private const int DefaultGalleryColumnCount = 4;

        /// <summary>
        /// Default value of the gallery thubnail size.
        /// </summary>
        private const int DefaultGalleryThumbnailSize = 100;

        /// <summary>
        /// Default value of the gallery size.
        /// </summary>
        private const int DefaultGallerySize = 500;

        /// <summary>
        /// Default value of the gallery thumbnail margin size.
        /// </summary>
        private const int DefaultGalleryThumbnailMarginSize = 3;

        /// <summary>
        /// Default value of the gallery thumbnail margin factor.
        /// </summary>
        private const int DefaultGalleryThumbnailMarginFactor = 10;
        

        #region PropertyName Consts

        /// <summary>
        /// The <see cref="SelectedPicture" /> property's name.
        /// </summary>
        public const string SelectedPicturePropertyName = "SelectedPicture";

        /// <summary>
        /// The <see cref="GalleryColumnCount" /> property's name.
        /// </summary>
        public const string GalleryColumnCountPropertyName = "GalleryColumnCount";

        /// <summary>
        /// The <see cref="GalleryThumbnailSize" /> property's name.
        /// </summary>
        public const string GalleryThumbnailSizePropertyName = "GalleryThumbnailSize";

        /// <summary>
        /// The <see cref="GalleryThumbnailMarginSize" /> property's name.
        /// </summary>
        public const string GalleryThumbnailMarginSizePropertyName = "GalleryThumbnailMarginSize";

        /// <summary>
        /// The <see cref="GallerySize" /> property's name.
        /// </summary>
        public const string GallerySizePropertyName = "GallerySize";
        
        #endregion
        #endregion

        #region Properties

        /// <summary>
        /// Gets the SelectedPicture property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public PicturePropertiesViewModel SelectedPicture {
            get {
                return _selectedPicture;
            }

            set {
                if (_selectedPicture == value) {
                    return;
                }

                var oldValue = _selectedPicture;
                _selectedPicture = value;

                //RaisePropertyChanged(SelectedPicturePropertyName);
                RaisePropertyChanged(SelectedPicturePropertyName, oldValue, _selectedPicture, true);
            }
        }

        /// <summary>
        /// Gets the collection of pictures property.
        /// </summary>
        public ObservableCollection<PicturePropertiesViewModel> Pictures {
            get {
                if (_pictures == null)
                    _pictures = new ObservableCollection<PicturePropertiesViewModel>();

                return _pictures;
            }

            set { _pictures = value; }
        }

        /// <summary>
        /// Gets the gallery column count property.
        /// </summary>
        public int GalleryColumnCount {
            get {
                if (_galleryColumnCount == 0)
                    _galleryColumnCount = DefaultGalleryColumnCount;

                return _galleryColumnCount;
            }
            set { 
                _galleryColumnCount = value;
                RaisePropertyChanged(GalleryColumnCountPropertyName);                
            }
        }

        /// <summary>
        /// gets the gallery size property.
        /// </summary>
        public int GallerySize {
            get {           
                return _gallerySize;
            }
            set {
                _gallerySize = value;
                GalleryThumbnailSize = _galleryThumbnailSize;
                RaisePropertyChanged(GallerySizePropertyName);                
            }
        }

        /// <summary>
        /// Gets the gallery thumbnail size property.
        /// </summary>
        public int GalleryThumbnailSize {
            get {
                if (_galleryThumbnailSize == 0)
                    _galleryThumbnailSize = DefaultGalleryThumbnailSize;

                return _galleryThumbnailSize;
            }
            set { 
                _galleryThumbnailSize = value;
                _galleryThumbnailMarginSize = (_galleryThumbnailSize / DefaultGalleryThumbnailMarginFactor);

                _galleryColumnCount = (_gallerySize - _galleryThumbnailMarginSize) / (_galleryThumbnailSize + _galleryThumbnailMarginSize);
                if (_galleryColumnCount < 1)
                    _galleryColumnCount = 1;
                RaisePropertyChanged(GalleryThumbnailSizePropertyName);
                RaisePropertyChanged(GalleryColumnCountPropertyName);                
                RaisePropertyChanged(GalleryThumbnailMarginSizePropertyName);
            }
        }

        /// <summary>
        /// Gets the gallery thumbnail margin size property.
        /// </summary>
        public int GalleryThumbnailMarginSize {
            get {
                if (_galleryThumbnailMarginSize == 0)
                    _galleryThumbnailMarginSize = DefaultGalleryThumbnailMarginSize;

                return _galleryThumbnailMarginSize;
            }
            set { 
                _galleryThumbnailMarginSize = value;
                RaisePropertyChanged(GalleryThumbnailMarginSizePropertyName);
            }
        }        

        #region Commands
        #endregion

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the GalleryViewModel class.
        /// </summary>
        /// <param name="pictures">The pictures.</param>
        public GalleryViewModel(ObservableCollection<PicturePropertiesViewModel> pictures ) {

            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.UriSource = new Uri("pack://application:,,,/PPP;component/icons/gui/gallery.png");
            bi.EndInit();
            IconSource = bi;

            Title = "Gallery";

            _pictures = pictures;
            
            if(_pictures != null && _pictures.Count > 0)
                _selectedPicture = Pictures[0];            
        }

        #endregion

        #region Methods
        #endregion

        #region Events
        #endregion

    }
}
